﻿// BooTasky is a small tools which use RememberTheMilk web services.
// Copyright (C) BooTasky of Yan-ren Tsai
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

namespace BooTasky

import System
import System.Collections.Generic
import System.Drawing
import System.Windows.Forms
import System.Diagnostics
import System.Net
import RtmNet

partial class MainForm:
	_exitFlag as bool = false
	_preference as Preference
	_service as IService
	_category as List[of ICategory]
	_tasks as List[of ITask]

	_paddingRight = 0
	
	def constructor():
		// The InitializeComponent() call is required for Windows Forms designer support.
		InitializeComponent()
		
		_preference = Preference.Instance
		_service = _preference.GetService()
		_preference.Load()
		
		if _service isa IAuthentication:
			auth = _service as IAuthentication
			if not auth.IsAuthenticated:
				if auth.StartAuthentication():
					auth.EndAuthentication()
				else:
					MessageBox.Show("Authentication is canceled.")
		
		_service.Initialize()
		
		_paddingRight = self.Width - (button1.Left + button1.Width)
		
		_category = List[of ICategory](_service.GetCategory())
		comboBox1.DataSource = _category
		//comboBox1.DisplayMember = "Name"
		makeDataGridView()

	protected def makeDataGridView():
		// Initialize and add a text box column.
		column = DataGridViewTextBoxColumn( DataPropertyName: "Name", Name: "Task", AutoSizeMode: DataGridViewAutoSizeColumnMode.DisplayedCells, ReadOnly: true, Resizable: DataGridViewTriState.True)
		dgvTasks.Columns.Add(column);

		// Initialize and add a check box column.
		//column = new DataGridViewCheckBoxColumn();
		//column.DataPropertyName = "";
		//column.Name = "Complete";
		//dataGridView1.Columns.Add(column);
	
	private def Button1Click(sender as object, e as System.EventArgs):
		MessageBox.Show("Not implemented.")
	
	private def MainFormResize(sender as object, e as System.EventArgs):
		control = sender as Control
		button1.Left = control.Width - _paddingRight - button1.Width 
		comboBox1.Width = button1.Left - _paddingRight - comboBox1.Left
	
	private def MainFormFormClosing(sender as object, e as System.Windows.Forms.FormClosingEventArgs):
		_preference.Save()
		if not _exitFlag:
			e.Cancel = true
			self.Hide()
	
	private def ComboBox1SelectedIndexChanged(sender as object, e as System.EventArgs):
		comboBox = sender as ComboBox

		selectedCategory = comboBox.SelectedValue as ICategory
		tasks = task for task in _service.GetTasks( selectedCategory.ID ) if task.State==TaskState.Active
		_tasks = List[of ITask]( tasks )
		_tasks.Sort( TaskComparer() )
		
		// Populate the data source.
		bindingSource1.DataSource = _tasks
		
		// Show the count of tasks
		notifyIcon1.BalloonTipText = "There are still ${_tasks.Count} tasks in ${selectedCategory.Name}."
		notifyIcon1.ShowBalloonTip(30)

	private def NotifyIcon1Click(sender as object, e as System.EventArgs):
		self.Show()
	
	private def MiExitClick(sender as object, e as System.EventArgs):
		_exitFlag=true
		self.Close()
	
	private def DgvTasksRowPrePaint(sender as object, e as System.Windows.Forms.DataGridViewRowPrePaintEventArgs):
		e.PaintParts &= ~DataGridViewPaintParts.Focus
		if e.RowIndex>=dgvTasks.Rows.Count-1:
			return
		task = dgvTasks.Rows[e.RowIndex].DataBoundItem as ITask 
		color = Color.White
		try:
			if ((e.State & DataGridViewElementStates.Selected)==DataGridViewElementStates.Selected):
				color = Color.Aqua
				rowBounds = Rectangle( dgvTasks.RowHeadersWidth, e.RowBounds.Top, dgvTasks.Columns.GetColumnsWidth( DataGridViewElementStates.Visible) - dgvTasks.HorizontalScrollingOffset + 1, e.RowBounds.Height)
	
				using lbrush=System.Drawing.Drawing2D.LinearGradientBrush( rowBounds, dgvTasks.DefaultCellStyle.SelectionBackColor, e.InheritedRowStyle.ForeColor, System.Drawing.Drawing2D.LinearGradientMode.Horizontal):
					e.Graphics.FillRectangle(lbrush, rowBounds)
			else:
				if task.Priority==TaskPriority.High:
					color = Color.LightPink 
				elif task.Priority==TaskPriority.Medium:
					color = Color.LightBlue
				elif task.Priority==TaskPriority.Low:
					color = Color.LightSkyBlue
				using brush=SolidBrush( color ):
					e.Graphics.FillRectangle( brush, e.RowBounds )
					e.PaintCellsContent( e.ClipBounds )
					e.Handled = true
		except e as Exception:
			Debug.WriteLine( e.Message )
		
[STAThread]
def Main(argv as (string)):
	Application.EnableVisualStyles()
	Application.SetCompatibleTextRenderingDefault(false)
	Application.Run(MainForm())
